package com.example.androidstudy.ch08;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class FreeLine extends Activity {
	MyView mv;
	ArrayList<VertexEntity>arrList;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mv = new MyView(this);
		setContentView(mv);
		arrList=new ArrayList<VertexEntity>();
	
	}
	
	
	public class MyView extends View{
		Paint mPaint;
		public MyView(Context context) {
			super(context);
			mPaint = new Paint();
			mPaint.setColor(Color.BLACK);
			mPaint.setStrokeWidth(3);
			mPaint.setAntiAlias(true);
			
		}

		@Override
		protected void onDraw(Canvas canvas) {
			canvas.drawColor(Color.LTGRAY);
			
			for(int i=0;i<arrList.size();i++){
				if(arrList.get(i).isDraw()){
					canvas.drawLine(arrList.get(i-1).getX(), arrList.get(i-1).getY(), arrList.get(i).getX(), arrList.get(i).getY(), mPaint);
				}
				
			}
		}
		
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			if(event.getAction()==MotionEvent.ACTION_DOWN){
			Toast.makeText(FreeLine.this, "action_down("+event.getX()+","+event.getY(), 1000);
			arrList.add(new VertexEntity(event.getX(), event.getY(), false));
			return true;
		}
		if(event.getAction()==MotionEvent.ACTION_MOVE){
			Toast.makeText(FreeLine.this, "action_move("+event.getX()+","+event.getY(), 1000);
			arrList.add(new VertexEntity(event.getX(), event.getY(), false));
			invalidate();
			return true;
		}
		return false;
	  }
	
   }
}